//
//  HttpPostMultipart.m
//  KangarooDoctor
//
//  Created by 廖罡 on 2024/2/1.
//

#import "HttpPostMultipart.h"

@implementation HttpPostMultipart

-(id)initWithUrl:(NSString *)urlString{
  self.request = [[NSMutableURLRequest alloc]init];
  self.urlString = urlString;
  self.headers = [[NSMutableDictionary alloc]init];
  self.fields = [[NSMutableDictionary alloc]init];
  self.files = [[NSMutableDictionary alloc]init];
  return self;
}

-(void)addHeader:(NSString *)key value:(NSString *)value{
  [self.headers setObject:value forKey:key];
}

-(void)addFormField:(NSString *)key value:(NSString *)value{
  [self.fields setObject:value forKey:key];
}

-(void)addFormFile:(NSString *)key path:(NSString *)path{
  [self.files setObject:path forKey:key];
}

-(void)addFormFileData:(NSString *)key data:(NSData *)data
{
    self.fileData = data;
}

- (void)finish:(void (^)(NSData *, NSURLResponse *, NSError *))callback{
  NSURL *url = [[NSURL alloc]initWithString:self.urlString];
  [self.request setURL:url];
  [self.request setHTTPMethod:@"POST"];
  // Add haders
  for (NSString *key in self.headers){
    NSString *value = self.headers[key];
    [self.request setValue:value forHTTPHeaderField:key];
  }
  // Add Content-Type
  
    NSString *boundary = @"547450202847502316249238";
  
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  
    [self.request setValue:contentType forHTTPHeaderField:@"Content-Type"];
    
  // Add fields
  NSMutableData *body = [NSMutableData data];
    
  for (NSString *key in self.fields){
    NSString *value = self.fields[key];
    // add params (all params are strings)
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", value] dataUsingEncoding:NSUTF8StringEncoding]];
  }
    
  // Add file
  for (NSString *key in self.files){
    NSString *path = self.files[key];
    NSString *fileName = [path lastPathComponent];
    
    NSString *fileExtension = [path pathExtension];
      NSString *contentType = @"image/png"; //[self getMimeType:fileExtension];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", key, fileName] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithContentsOfFile:path]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  }
    
    if (self.fileData != nil) {
        NSString *contentType = @"image/png";
        
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"photo.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:self.fileData];
        
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
  
  if ([body length] > 0) {
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [self.request setHTTPBody:body];
    
    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%ld", [body length]];
    [self.request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  }
  
  void (^completionHandler)(NSData *, NSURLResponse *, NSError *) = ^(NSData *data, NSURLResponse *response, NSError *error) {
    if (callback) {
      callback(data, response, error);
    }
  };
  
    
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    
    config.HTTPShouldUsePipelining = YES;
    
    
  [[[NSURLSession sessionWithConfiguration:config] dataTaskWithRequest:self.request completionHandler:completionHandler] resume];
    
//    [[NSURLSession sharedSession] uploadTaskWithRequest:nil fromFile:nil completionHandler:nil];
    
}


#if DEBUG
+(NSDictionary*)connectionProxyDictionary
{
    NSString* host = @"192.168.5.4";
//    NSString* host = @"127.0.0.1";
    
    NSNumber* port = [NSNumber numberWithInt: 8888];
    return @{
        @"HTTPEnable":@(1),
        (NSString*)kCFStreamPropertyHTTPProxyHost: host,
        (NSString*)kCFStreamPropertyHTTPProxyPort: port,
        
        @"HTTPSEnable":@(1),
        (NSString*)kCFStreamPropertyHTTPSProxyHost: host,
        (NSString*)kCFStreamPropertyHTTPSProxyPort: port,
        
    };
}
#endif

@end
